package com.ctg.payroll2.action.xlsImport;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONArray;

import org.apache.struts2.ServletActionContext;

import com.ctg.payroll2.bean.po.ExcelInterfaceDetail;
import com.ctg.payroll2.bean.po.ExcelInterfaceMaster;
import com.ctg.payroll2.bean.po.ExcelPublicMaster;
import com.ctg.payroll2.bean.po.ExcelpublicDetail;
import com.ctg.payroll2.bean.po.SysUser;
import com.ctg.payroll2.service.xlsImport.ExeclCustomizeService;
import com.ctg.payroll2.service.xlsImport.XlsImportService;
import com.ctg.payroll2.util.Constants;
import com.ctg.platform.util.Page;
import com.googlecode.jsonplugin.annotations.JSON;
import com.opensymphony.xwork2.ActionSupport;

public class ExeclCustomizeAction extends ActionSupport {
	private ExeclCustomizeService customizeService;

	private Page publicMaster;

	private Page publicDetial;

	private Page publicInteface;

	private Page publicIntefaceDetial;

	private Page payClassIds;

	private Page payClassItems;

	private Page socialItems;

	private List list;

	private XlsImportService xlsImportService;

	public List getList() {
		return list;
	}

	public void setList(List list) {
		this.list = list;
	}

	@JSON(serialize = false)
	public XlsImportService getXlsImportService() {
		return xlsImportService;
	}

	public void setXlsImportService(XlsImportService xlsImportService) {
		this.xlsImportService = xlsImportService;
	}

	public Page getSocialItems() {
		return socialItems;
	}

	public void setSocialItems(Page socialItems) {
		this.socialItems = socialItems;
	}

	public Page getPayClassItems() {
		return payClassItems;
	}

	public void setPayClassItems(Page payClassItems) {
		this.payClassItems = payClassItems;
	}

	public Page getPayClassIds() {
		return payClassIds;
	}

	public void setPayClassIds(Page payClassIds) {
		this.payClassIds = payClassIds;
	}

	public Page getPublicInteface() {
		return publicInteface;
	}

	public void setPublicInteface(Page publicInteface) {
		this.publicInteface = publicInteface;
	}

	public Page getPublicIntefaceDetial() {
		return publicIntefaceDetial;
	}

	public void setPublicIntefaceDetial(Page publicIntefaceDetial) {
		this.publicIntefaceDetial = publicIntefaceDetial;
	}

	public Page getPublicMaster() {
		return publicMaster;
	}

	public void setPublicMaster(Page publicMaster) {
		this.publicMaster = publicMaster;
	}

	public Page getPublicDetial() {
		return publicDetial;
	}

	public void setPublicDetial(Page publicDetial) {
		this.publicDetial = publicDetial;
	}

	@JSON(serialize = false)
	public ExeclCustomizeService getCustomizeService() {
		return customizeService;
	}

	public void setCustomizeService(ExeclCustomizeService customizeService) {
		this.customizeService = customizeService;
	}

	public String insert() {

		return SUCCESS;
	}

	/**
	 * 基础项设定的查询操作 支持按条件查询
	 * 
	 * @return
	 */
	public String queryPublicMasterByCondition() {
		Map<String, Object> publicMasterMap = new HashMap<String, Object>();
		HttpServletRequest request = ServletActionContext.getRequest();
		if (!"0000".equals(request.getParameter("exPublicType"))) {
			publicMasterMap.put("exPublicType", request
					.getParameter("exPublicType"));
		}
		if (!"-1".equals(request.getParameter("isUsed"))) {
			publicMasterMap.put("isUsed", request.getParameter("isUsed"));
		}
		publicMasterMap.put("exPublicName", request
				.getParameter("exPublicName"));
		publicMaster = customizeService.queryPublicMaster(publicMasterMap, 0,
				100);
		return SUCCESS;
	}

	/**
	 * 通过传过来得exPublicid查找该基础项下面的所有子项信息并在页面显示
	 * 
	 * @return
	 */
	public String queryPublicDetail() {
		Map<String, Object> publicDetailPara = new HashMap<String, Object>();
		HttpServletRequest request = ServletActionContext.getRequest();
		publicDetailPara.put("exPublicid", request.getParameter("exPublicid"));
		publicDetailPara.put("exPublicType", request
				.getParameter("exPublicType"));
		if (null != request.getParameter("isNecessary")
				|| !"".equals(request.getParameter("isNecessary"))) {
			publicDetailPara.put("isNecessary", request
					.getParameter("isNecessary"));
		}
		publicDetial = customizeService.queryPublicDetailsl(publicDetailPara,
				0, 100);
		return SUCCESS;
	}

	/**
	 * 根据在grid中勾选的exPublicid删除该项目
	 * 
	 * @return
	 */
	public String deleteExcelPublicMaster() {
		HttpServletRequest request = ServletActionContext.getRequest();
		String exPublicIds = request.getParameter("exPublicIds");
		String[] array = splitStringToStringArray(exPublicIds);
		for (int i = 0; i < array.length; i++) {
			if (customizeService.deleteByExPublicId(Integer.parseInt(array[i]))) {
				customizeService.deletePublicMaster(Integer.parseInt(array[i]));
			}
		}

		return SUCCESS;
	}

	/**
	 * 把string字符串转化为数组
	 * 
	 * @param args
	 * @return
	 */
	public static String[] splitStringToStringArray(String args) {
		JSONArray json = JSONArray.fromObject(args);
		Object[] ss = json.toArray();
		String[] array = new String[ss.length];
		for (int i = 0; i < ss.length; i++) {
			String s = ss[i] + "";
			array[i] = s;
		}
		return array;
	}

	/**
	 * 增加和修改基础项信息
	 * 
	 * @return
	 */
	public String insertOrUpdatePublicMaster() {

		try {
			HttpServletRequest request = ServletActionContext.getRequest();
			String exPublictype = request.getParameter("exPublictype");
			String isUsed = request.getParameter("isUsed");
			String exPublicName = request.getParameter("exPublicName");
			String exMemo = request.getParameter("exMemo");
			String exPublicId = request.getParameter("exPublicId");
			String exlPublicDetail = request.getParameter("exlPublicDetail");
			List list = customizeService.getBeanListfromJson(exlPublicDetail,
					ExcelpublicDetail.class, "detailStr");
			ExcelPublicMaster epm = new ExcelPublicMaster();
			epm.setExPublicType(exPublictype);
			epm.setIsUsed(Integer.parseInt(isUsed));
			epm.setExPublicName(exPublicName);
			epm.setExMemo(exMemo);

			// exPublicId不存在表示为新增的记录，则直接插入一条新的数据
			if (null == exPublicId || "" == exPublicId) {
				Integer newId = customizeService.getNewId();
				epm.setExPublicId(newId);
				customizeService.insertPublicMaster(epm);
			} else {
				// 否则，则表示为已存在的记录，
				epm.setExPublicId(Integer.parseInt(exPublicId));
				customizeService.updatePublicMaster(epm);
			}

			ExcelpublicDetail detail;
			for (int i = 0; i < list.size(); i++) {
				detail = (ExcelpublicDetail) list.get(i);
				// 如果getExPublicid为空，则直接插入新的记录
				if (null == detail.getExSubId()
						|| "".equals(detail.getExSubId())) {
					detail.setExPublicid(epm.getExPublicId());
					detail.setExSubId(customizeService.getNewId());
					detail.setExPublicType(exPublictype);
					customizeService.insertPublicDetailsl(detail);
				} else {
					// 否则根据修改原来的记录
					customizeService.updatePublicDetailsl(detail);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return SUCCESS;
	}

	/**
	 * 通过exPublicId删除该项记录以及所有该项的子记录
	 * 
	 * @return
	 */
	public String deleteAll() {
		try {
			HttpServletRequest request = ServletActionContext.getRequest();
			String exPublicId = request.getParameter("exPublicId");
			if (null != exPublicId || !"".equals(exPublicId)) {

				// 删除所有的子项记录
				if (customizeService.deleteByExPublicId(Integer
						.parseInt(exPublicId))) {
					// 删除该记录
					customizeService.deletePublicMaster(Integer
							.parseInt(exPublicId));
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}

	/**
	 * 根据勾选的ExSubId删除详细信息表里的项目
	 */
	public String deleteDetails() {
		try {
			HttpServletRequest request = ServletActionContext.getRequest();
			String ids = request.getParameter("subIds");
			List list = customizeService.getBeanListfromJson(ids,
					ExcelpublicDetail.class, "detailIds");
			ExcelpublicDetail detail;
			for (int i = 0; i < list.size(); i++) {
				detail = (ExcelpublicDetail) list.get(i);
				customizeService.deletePublicDetailsl(detail.getExSubId());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}

	/**
	 * 查询Excel文件类别下拉框的数据
	 * 
	 * @return
	 */
	public String getAllExlType() {
		// customizeService.
		return SUCCESS;
	}

	// 接口的操作————————————————————————————————————————————————————————————————
	/**
	 * 根据查询条件查询导入接口的详细信息
	 */
	public String queryInterfaceMasterByCondition() {
		Map<String, Object> publicIntefaceMap = new HashMap<String, Object>();
		HttpServletRequest request = ServletActionContext.getRequest();
		SysUser su = (SysUser) request.getSession().getAttribute(
				Constants.CURRENT_USER);
		String company_chinese_code = su.getCompanyCode();
		if (company_chinese_code != null || !"".equals(company_chinese_code)) {
			publicIntefaceMap.put("companyCode", company_chinese_code);
		}
		String exNameSelect = request.getParameter("exNameSelect");
		if (exNameSelect != null || !"".equals(exNameSelect)) {
			publicIntefaceMap.put("exName", exNameSelect);
		}
		String typeId = request.getParameter("typeId");
		if (typeId != null || !"".equals(typeId)) {
			publicIntefaceMap.put("exPublicType", typeId);
		}
		try {
			publicInteface = customizeService.queryInterfaceMaster(
					publicIntefaceMap, 0, 100);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return SUCCESS;
	}

	/**
	 * 根据checkbox中选择的Id删除接口表中的记录
	 * 
	 * @return
	 */
	public String deleteExcelIntefaceMaster() {
		try {
			HttpServletRequest request = ServletActionContext.getRequest();
			String exIntefaceIds = request.getParameter("exIntefaceIds");
			String[] array = splitStringToStringArray(exIntefaceIds);
			for (int i = 0; i < array.length; i++) {
				if (customizeService
						.deleteInterfacedetailByexIntefaceId(Integer
								.parseInt(array[i]))) {
					customizeService.deleteInterFaceMaster(Integer
							.parseInt(array[i]));
				}
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}

	/**
	 * 根据Id查找相应子项
	 * 
	 * @return
	 */
	public String queryIntefaceDetail() {
		Map<String, Object> publicInterfacePara = new HashMap<String, Object>();
		HttpServletRequest request = ServletActionContext.getRequest();
		publicInterfacePara.put("exInterfaceId", request
				.getParameter("exInterfaceId"));
		publicIntefaceDetial = customizeService.queryInterfaceDetailsl(
				publicInterfacePara, 0, 100);
		return SUCCESS;
	}

	/**
	 * 如果选择的excell文件类别为薪资类别，则在查询薪资的对应Id赋值到对应Id的下拉框里
	 * 
	 * @return
	 */
	public String findExClassIdByCompanyCode() {
		Map<String, Object> payClassIdsParam = new HashMap<String, Object>();
		HttpServletRequest request = ServletActionContext.getRequest();
		SysUser su = (SysUser) request.getSession().getAttribute(
				Constants.CURRENT_USER);
		payClassIdsParam.put("companyCode", su.getCompanyCode());
		payClassIds = customizeService.queryPayClassByCompanyCode(
				payClassIdsParam, 0, 200);
		return SUCCESS;
	}

	/**
	 * 当excell文件类别选择的是薪资项目的时候，根据选择的对应Id查询出所有的输入项的名称显示到’字段名‘的下拉框中
	 * 
	 * @return
	 */
	public String queryPayClassItemsByPayClassId() {
		Map<String, Object> itemsParam = new HashMap<String, Object>();
		HttpServletRequest request = ServletActionContext.getRequest();
		SysUser su = (SysUser) request.getSession().getAttribute(
				Constants.CURRENT_USER);
		itemsParam.put("payClassId", request.getParameter("payClassId"));
		itemsParam.put("companyCode", su.getCompanyCode());
		payClassItems = customizeService.queryPayClassItemsByPayClassId(
				itemsParam, 0, 200);
		return SUCCESS;
	}

	/**
	 * 如果excell文件类别是社保项目的时候，则根据公司Id选择所有是社保的输入项显示到’字段名‘的下拉框中
	 * 
	 * @return
	 */
	public String querySocialByCompanyCode() {
		Map<String, Object> socialParam = new HashMap<String, Object>();
		HttpServletRequest request = ServletActionContext.getRequest();
		SysUser su = (SysUser) request.getSession().getAttribute(
				Constants.CURRENT_USER);
		socialParam.put("companyCode", su.getCompanyCode());
		socialItems = customizeService.querySocialByCompanyCode(socialParam, 0,
				200);
		return SUCCESS;
	}

	/**
	 * 增加和修改接口信息
	 * 
	 * @return
	 */
	public String insertOrUpdatePublicInteface() {
		try {
			HttpServletRequest request = ServletActionContext.getRequest();
			SysUser su = (SysUser) request.getSession().getAttribute(
					Constants.CURRENT_USER);
			String exPublictype = request.getParameter("exPublictype");
			String exClassId = request.getParameter("exClassId");
			String exName = request.getParameter("exName");
			String memo = request.getParameter("memo");
			String exInterfaceId = request.getParameter("exInterfaceId");
			String intefaceDetail = request.getParameter("intefaceDetail");
			List list = customizeService.getBeanListfromJson(intefaceDetail,
					ExcelInterfaceDetail.class, "interfaceStr");
			// System.out.println("intefaceDetail : " + intefaceDetail);
			ExcelInterfaceMaster eim = new ExcelInterfaceMaster();
			eim.setCompanyCode(su.getCompanyCode());
			eim.setExPublicType(exPublictype);
			eim.setExName(exName);
			if (exPublictype.equals("1015")) {
				eim.setExClassId(Long.parseLong(exClassId));
			}
			eim.setExStart(2);
			eim.setMemo(memo);
			// exInterfaceId不存在表示为新增的记录，则直接插入一条新的数据
			if (null == exInterfaceId || "" == exInterfaceId) {
				Integer newId = customizeService.getNewId();
				eim.setExInterfaceId(newId);
				customizeService.insertInterFaceMaster(eim);
			} else {
				// 否则，则表示为已存在的记录，
				eim.setExInterfaceId(Integer.parseInt(exInterfaceId));
				customizeService.updateInterFaceMaster(eim);
			}

			ExcelInterfaceDetail inDetail;
			for (int i = 0; i < list.size(); i++) {
				inDetail = (ExcelInterfaceDetail) list.get(i);
				// 如果getExSubInterfaceId为空，则直接插入新的记录
				if (null == inDetail.getExSubInterfaceId()
						|| "".equals(inDetail.getExSubInterfaceId())) {
					inDetail.setExInterfaceId(Integer.parseInt(eim
							.getExInterfaceId().toString()));
					inDetail.setExSubInterfaceId(Integer
							.parseInt(customizeService.getNewId().toString()));
					customizeService.insertInterFaceDetailsl(inDetail);
				} else {
					// 否则根据修改原来的记录
					customizeService.updateInterFaceDetailsl(inDetail);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}

	/**
	 * 在接口新增和修改界面根据Id删除该Id下的所有东西
	 * 
	 * @return
	 */
	public String deleteByInterfaceId() {
		try {
			HttpServletRequest request = ServletActionContext.getRequest();
			String exInterfaceId = request.getParameter("exInterfaceId");
			if (null != exInterfaceId || !"".equals(exInterfaceId)) {

				// 删除所有的子项记录
				if (customizeService
						.deleteInterfacedetailByexIntefaceId(Integer
								.parseInt(exInterfaceId))) {
					// 删除该记录
					customizeService.deleteInterFaceMaster(Integer
							.parseInt(exInterfaceId));
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}

	/**
	 * 根据勾选的ExSubIntefaceId删除详细信息表里的项目
	 */
	public String deleteSubInterfaceById() {
		try {
			HttpServletRequest request = ServletActionContext.getRequest();
			String ids = request.getParameter("exSubInterfaceId");
			List list = customizeService.getBeanListfromJson(ids,
					ExcelInterfaceDetail.class, "exSubInterfaceIds");
			ExcelInterfaceDetail detail;
			for (int i = 0; i < list.size(); i++) {
				detail = (ExcelInterfaceDetail) list.get(i);
				customizeService.deleteInterFaceDetailsl(detail
						.getExSubInterfaceId());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}

	/**
	 * 导入接口为薪资类别是检查接口细项是否可用
	 * 
	 * @return
	 */
	public String payClassAndSocialValid() {
		HttpServletRequest request = ServletActionContext.getRequest();
		String id = request.getParameter("id");
		String type = request.getParameter("type");
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("ex_interfaceid", id);
		try {
			if (type.equals("1015")) {
				list = xlsImportService.payClassIsValid(map);
			} else if (type.equals("1017")) {
				list = xlsImportService.socialIsValid(map);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}
}
